--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--开服连续充值

--sOpenServerContinuousRechargeData = 208, 	--开服连续充值
function OpenServerContinuousRechargeData(sysarg, packet)
	if not IsInOpenServerDay() then
		return
	end
	local opType = DataPack.readByte(packet)
	if opType == 0 then
		SendOpenServerContinuousRecharge(sysarg)
	elseif opType == 1 then
		local ContinuousRechargeCfg = OpenServerCfg.ContinuousRecharge
		local actorid = Actor.getActorId(sysarg)
		local data = gRankMgr_GetValueById(actorid, RANK_OS_CONTINUOUS_RECHARGE, 1)
		local myData = BaseFuc_unserialize(data)

		if not myData then
			myData = {}
		end

		if not myData.oSContinuousRecharge then
			myData.oSContinuousRecharge = 0
		end
		if not myData.oSContinuousRechargeState then
			myData.oSContinuousRechargeState = 0
		end
		local days = System.getDaysSinceOpenServer()
		if myData.oSContinuousRecharge >= ContinuousRechargeCfg.needRechargeDays and days >= ContinuousRechargeCfg.canGetDay and myData.oSContinuousRechargeState == 0 then
			myData.oSContinuousRechargeState = 1
		end
		if myData.oSContinuousRechargeState == 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerContinuousRecharge003, ttFlyTip)
			return
		elseif myData.oSContinuousRechargeState == 2 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerContinuousRecharge004, ttFlyTip)
			return
		end
		myData.oSContinuousRechargeState = 2

		local newData = BaseFuc_serialize(myData)
		gRankMgr_SetRankEx(actorid, RANK_OS_CONTINUOUS_RECHARGE, 1, newData)


		local rewards = ContinuousRechargeCfg.exAwards
		if IsBagGridEnough(sysarg, rewards) then
			GiveCommonAward(sysarg, rewards, LogIds.OpenServerContinuousRechargeLogId, Lang.LogTips.OpenServerContinuousRecharge)
		else
			local actorId = Actor.getActorId(sysarg)
			local title = Lang.ScriptTips.OpenServerContinuousRecharge001
			local content = Lang.ScriptTips.OpenServerContinuousRecharge002

			local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
	        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

			Awards.GiveToMailById(actorId, rewards, title, content, LogIds.OpenServerContinuousRechargeLogId, Lang.LogTips.OpenServerContinuousRecharge, vocation, sex)
		end	

		SendOpenServerContinuousRecharge(sysarg)
	end
end

function SendOpenServerContinuousRecharge(sysarg)
	if not IsInOpenServerDay() then
		return
	end
	local ContinuousRechargeCfg = OpenServerCfg.ContinuousRecharge
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_OS_CONTINUOUS_RECHARGE, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData then
		myData = {}
	end

	if not myData.oSContinuousRecharge then
		myData.oSContinuousRecharge = 0
	end
	if not myData.oSContinuousRechargeState then
		myData.oSContinuousRechargeState = 0
	end
	local todayRechargeTime = 0
	local time = System.getCurrMiniTime()
	if myData.oSContinuousRechargeTime and System.isSameDay(time, myData.oSContinuousRechargeTime) then
		todayRechargeTime = 1
	end
	local days = System.getDaysSinceOpenServer()
	if myData.oSContinuousRecharge >= ContinuousRechargeCfg.needRechargeDays and days >= ContinuousRechargeCfg.canGetDay and myData.oSContinuousRechargeState == 0 then
		myData.oSContinuousRechargeState = 1
	end

	local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sOpenServerContinuousRechargeData)
	if npack == nil then 
		return 
	end
	--print("myData.oSContinuousRecharge or 0 ="..(myData.oSContinuousRecharge or 0))
	DataPack.writeByte(npack, myData.oSContinuousRecharge or 0)
	--print("myData.oSContinuousRechargeState or 0 ="..(myData.oSContinuousRechargeState or 0))
	DataPack.writeByte(npack, myData.oSContinuousRechargeState or 0)
	--print("todayRechargeTime ="..todayRechargeTime)
	DataPack.writeByte(npack, todayRechargeTime)
	DataPack.flush(npack) 
end

-- 活动结束
function OpenServerContinuousRechargeEnd()
	if not IsInOpenServerEndDay() then
		return
	end
	local ContinuousRechargeCfg = OpenServerCfg.ContinuousRecharge
	if not ContinuousRechargeCfg then
		return
	end
	local rankPtr = Ranking.getRanking(RankList[RANK_OS_CONTINUOUS_RECHARGE].strName)
	local rankCount = Ranking.getRankItemCount(rankPtr)
	for idx = 1, rankCount do
		local item = Ranking.getItemFromIndex(rankPtr, idx-1)
		if item then
			local actorid = tonumber(Ranking.getId(item))
			local data = Ranking.getSub(item, 1)
			local dataTable
			if not data or data == "" then
				dataTable = {}
			else
				dataTable = BaseFuc_unserialize(data)
			end
			if dataTable then
				if not dataTable.oSContinuousRecharge then
					dataTable.oSContinuousRecharge = 0
				end
				if not dataTable.oSContinuousRechargeState then
					dataTable.oSContinuousRechargeState = 0
				end
				if dataTable.oSContinuousRecharge >= ContinuousRechargeCfg.needRechargeDays and dataTable.oSContinuousRechargeState == 0 then
					dataTable.oSContinuousRechargeState = 1
				end
				if dataTable.oSContinuousRechargeState == 1 then
					local rewards = ContinuousRechargeCfg.exAwards
					local title = Lang.ScriptTips.OpenServerContinuousRecharge001
					local content = Lang.ScriptTips.OpenServerContinuousRecharge002
					Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OpenServerContinuousRechargeLogId, Lang.LogTips.OpenServerContinuousRecharge, dataTable.vocation, dataTable.sex)
				end
			end
		end
	end
end

function OpenServerContinuousRechargeWithDrawYuanBao(sysarg, nValue)
	if not IsInOpenServerDay() then
		return
	end
	if nValue <= 0 then
		return
	end
	--print("nValue ="..nValue)
	local ContinuousRechargeCfg = OpenServerCfg.ContinuousRecharge
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_OS_CONTINUOUS_RECHARGE, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData then
		myData = {}
	end
	
	if not myData.oSContinuousRecharge then
		myData.oSContinuousRecharge = 0
	end
	if not myData.oSContinuousRechargeNum then
		myData.oSContinuousRechargeNum = 0
	end
	local time = System.getCurrMiniTime()
	if not myData.oSContinuousRechargeTime then
		myData.oSContinuousRechargeTime = time
	end
	if not System.isSameDay(time, myData.oSContinuousRechargeTime) then
		myData.oSContinuousRechargeNum = 0
	elseif myData.oSContinuousRechargeNum >= ContinuousRechargeCfg.dailyRechargeNum then
		myData.oSContinuousRechargeNum = myData.oSContinuousRechargeNum + nValue

		local newData = BaseFuc_serialize(myData)
		gRankMgr_SetRankEx(actorid, RANK_OS_CONTINUOUS_RECHARGE, 1, newData)
		return
	end
	myData.oSContinuousRechargeNum = myData.oSContinuousRechargeNum + nValue
	myData.oSContinuousRechargeTime = time
	if myData.oSContinuousRechargeNum >= ContinuousRechargeCfg.dailyRechargeNum then
		myData.oSContinuousRecharge = myData.oSContinuousRecharge + 1
		local awards = ContinuousRechargeCfg.Awards[myData.oSContinuousRecharge]
		if awards then
			local title = Lang.ScriptTips.OpenServerContinuousRecharge001
			local content = Lang.ScriptTips.OpenServerContinuousRecharge002

			local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
	        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

			Awards.GiveToMailById(actorid, awards, title, content, LogIds.OpenServerContinuousRechargeLogId, Lang.LogTips.OpenServerContinuousRecharge, vocation, sex)
		end
	end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end

	local newData = BaseFuc_serialize(myData)
	gRankMgr_SetRankEx(actorid, RANK_OS_CONTINUOUS_RECHARGE, 1, newData)

	SendOpenServerContinuousRecharge(sysarg)
end


